package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.RedPacketsReturnMap;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.RedPacketsRecordView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class RedPacketsRecordPresenter extends BaseFragmentPresenter<RedPacketsRecordView> {

  public RedPacketsRecordPresenter(Activity activity,
      RedPacketsRecordView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 查询红包记录
   *
   * @param flag 0 收到的 1 发出的
   * @param startRow 开始行数
   * @param pageSize 每次获取条数
   */
  public void queryRedPacketsRecord(int flag, int startRow, int pageSize) {
    ApiClient.getApiRetrofitInstance()
        .queryRedPacketsRecord(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), flag, startRow, pageSize)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ShowTipObserver<RedPacketsReturnMap>(activity) {
          @Override public void onSuccess(RedPacketsReturnMap returnMap) {
            iView.queryRedPacketsRecord(returnMap);
          }
        });
  }
}
